/*
	Copyright (C) 2010 Rémi Thebault

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <QStyle>
#include <QPixmap>
#include <QPainter>
#include <QPen>
#include <QBrush>
#include <QColorDialog>


#include "qcolorbutton.h"

QColorButton::QColorButton(const QColor& initialColor, QWidget *parent) :
	QPushButton(parent),
	m_color(initialColor)
{
	resetIcon();
	QObject::connect(this, SIGNAL(clicked()), this, SLOT(userSelectColor()));
}


void QColorButton::userSelectColor() {
	QColor c = QColorDialog::getColor(m_color, this);
	if (c.isValid()) {
		m_color = c;
		resetIcon();
		emit selectedColorChanged(c);
	}
}

void QColorButton::resetIcon() {
	int height = style()->pixelMetric(QStyle::PM_ButtonIconSize);
	QSize s (2*height, height);
	QPixmap pm (s);
	QPainter painter (&pm);
	painter.setPen(QPen(Qt::black));
	painter.setBrush(QBrush(m_color));
	painter.drawRect(QRect(0, 0, s.width()-1, s.height()-1));
	this->setIcon(QIcon(pm));
}
